/* 
    generator 函数 返回的迭代器
    内部的 yield 除了可以给外部返回数据，还可以想内部传递数据
*/

const fs = require('fs');
const utils = require('util');
/* 把异步的文件读取 Promise */
const readFile = utils.promisify(fs.readFile);



function* run() {
    // yield 一个promise对象
    let a = yield readFile('./data/1.txt');
    console.log(a);

    let b = yield readFile('./data/2.txt');
    console.log(b);

    let c = yield readFile('./data/3.txt');
    console.log(c);

    return a + b + c;

}

const it = run();

/* it.next().value=== readFile('./data/1.txt')  */
it.next().value.then(data => {

    it.next(data.toString()).value.then(data => {

        it.next(data.toString()).value.then(data => {

            console.log(it.next(data.toString()));

        })

    })

})

/* readFile('./data/1.txt').then(data => {
    console.log(data);
}) */